package com.example.demo.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.*;
import org.hibernate.Hibernate;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

/**
 * 图书数据
 *
 * @author Liunh
 */
@DynamicInsert
@DynamicUpdate
@Entity
@Table(name = "T_BOOK")
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Book implements Serializable {

    @Transient
    private static final long serialVersionUID = 1L;

    /**
     * id
     */
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 当前架位
     */
    @Column(name = "currentLocation")
    private String currentLocation;

    /**
     * sysUniqueId + sysId
     */
    @Column(name = "uniqueId")
    private String uniqueId;

    /**
     */
    @Column(name = "currentStatusId")
    private Integer currentStatusId;

    /**
     * 批次号
     * 导入数据时才有值
     */
    @Column(name = "batchNo")
    private String batchNo;

    /**
     * 图书条码/主馆藏标识
     */
    @Column(name = "barCode")
    private String barCode;

    /**
     * 电子标签物理卡号
     */
    @Column(name = "bookTid")
    private String bookTid;

    /**
     * 所属馆机构号
     */
    @Column(name = "parentOrgCode")
    private Long parentOrgCode;
    @Column(name = "parentOrgName")
    private String parentOrgName;
    @Column(name = "orgCode")
    private Long orgCode;
    @Column(name = "orgName")
    private String orgName;
    @Column(name = "subOrgCode")
    private Long subOrgCode;
    @Column(name = "subOrgName")
    private String subOrgName;

    /**
     * 图书在所属图书管理系统/平台的唯一标识数据id
     */
    @Column(name = "sysUniqueId")
    private String sysUniqueId;

    /**
     * 所属图书管理系统/平台标识
     */
    @Column(name = "sysId")
    private String sysId;

    /**
     * 所属架位
     */
    @Column(name = "initLocation")
    private String initLocation;

    /**
     * 标签物理id
     */
    @Column(name = "rfId")
    private String rfId;

    /**
     * 书名
     */
    @Column(name = "title")
    private String title;

    /**
     * 作者
     */
    @Column(name = "author")
    private String author;

    /**
     * 作者编号
     */
    @Column(name = "authorNo")
    private String authorNo;

    /**
     * isbn
     */
    @Column(name = "isbn")
    private String isbn;

    /**
     * mrIsbn
     */
    @Column(name = "mrIsbn")
    private String mrIsbn;

    /**
     * 出版社
     */
    @Column(name = "publisher")
    private String publisher;

    /**
     * 分类id（指22大类id）；0未知
     */
    @Column(name = "categoryType")
    private Integer categoryType;

    /**
     * 分类号
     */
    @Column(name = "categoryNum")
    private String categoryNum;

    /**
     * 开本
     */
    @Column(name = "\"size\"")
    private String size;

    /**
     * 页码
     */
    @Column(name = "\"page\"")
    private String page;

    /**
     * 价格
     */
    @Column(name = "price")
    private BigDecimal price;

    /**
     * 书标/索书号
     */
    @Column(name = "bookLable")
    private String bookLable;

    /**
     * 出版社编号
     */
    @Column(name = "publisherCode")
    private String publisherCode;

    /**
     * 出版日期
     */
    @Column(name = "publishDate")
    private String publishDate;

    /**
     * 摘要
     */
    @Column(name = "remark")
    private String remark;

    /**
     * 翻译
     */
    @Column(name = "translator")
    private String translator;

    /**
     * 副标题
     */
    @Column(name = "subtitle")
    private String subTitle;

    /**
     * 封面图片
     */
    @Column(name = "coverUrl")
    private String coverUrl;

    /**
     * 媒体类型（0全部 1图书 2报刊/杂志 3多媒体）
     */
    @Column(name = "mediaType")
    private Integer mediaType;

    /**
     * 是否在架（0不在架；1在架）
     */
    @Column(name = "onShelf")
    private Integer onShelf;

    /**
     * 上架时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    //@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(name = "upShelfDate")
    private Date upShelfDate;

    /**
     * 下架时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    //@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(name = "offShelfDate")
    private Date offShelfDate;

    /**
     * 采集人id
     */
    @Column(name = "userId")
    private Integer userId;

    /**
     * 是否已计入统计(0没记入，1已记入)
     */
    @Column(name = "counted")
    private Integer counted;

    /**
     * 记录版本号；结合SysID+SYSUniqueID用于数据同步
     */
    @Column(name = "version")
    private Long version;

    /**
     * 数据来源类型：1采集；2导入
     */
    @Column(name = "sourceType")
    private Integer sourceType;
    /**
     * 数据来源说明
     */
    @Column(name = "sourceDesc")
    private String sourceDesc;

    /**
     * 创建时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(name = "createDate")
    private Date createDate;

    /**
     * 修改时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(name = "updateTime")
    private Date updateTime;

    public Book(String barCode) {
        this.barCode = barCode;
    }

    public Book(Long orgCode, String isbn, String barCode) {
        this.orgCode = orgCode;
        this.isbn = isbn;
        this.barCode = barCode;
    }

    public Book(String sysUniqueId, String sysId, Long orgCode, String isbn, String barCode) {
        this.sysUniqueId = sysUniqueId;
        this.sysId = sysId;
        this.orgCode = orgCode;
        this.isbn = isbn;
        this.barCode = barCode;
    }

    public Book(String sysUniqueId, String sysId) {
        this.sysUniqueId = sysUniqueId;
        this.sysId = sysId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) {
            return false;
        }
        Book book = (Book) o;
        return id != null && Objects.equals(id, book.id);
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}
